<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/3/23
 * Time: 11:02
 */
namespace app\admin\controller;
use think\Db;
class ShowController extends CommonController
{
    /**
     *getUserData 页面加载获取用户表数据
     * @return json格式数组
     */
    public function getData(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = $this->getDataByCampusid($_POST);
        $res = Db::table('ew_hot')->alias('h')
            ->join('ew_u_user u','h.h_user_id=u.user_id','LEFT')
            ->where($where)
            ->field('h.*,user_id,user_username,user_phone')
            ->order('h_top DESC,h_time DESC')
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total =Db::table('ew_hot')->alias('h')
            ->join('ew_u_user u','h.h_user_id=u.user_id','LEFT')
            ->where($where)
            ->field('user_id')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }


    /**
     * seeUserInfo 动态详情
     * @return 返回状态
     */
    public function seeInfo(){
        $hid = input('hid');
        $data = Db::table('ew_hot')->alias('h')
            ->join('ew_u_user u','h.h_user_id=u.user_id','LEFT')
            ->where('h_id',$hid)
            ->field('h.*,user_id,user_username,user_phone')
            ->find();
        $this->assign ('data',$data);
        return $this->fetch("show/show_info");
    }
    /**
     * 删除评论
     * cid
     * @return string
     */
    public function delete(){
        $hid = $_POST['hid'];
        // 启动事务
        Db::startTrans();
        try{
            //拿到数据之后删掉这条动态
            Db::table('ew_hot')->where('h_id',$hid)->delete();
            //删除掉这个动态的所有回复
            Db::table('ew_hot_comment_tt')->where('comtt_comment_id',$hid)->delete();
            Db::table('ew_hot_zan')->where('cz_hot_id',$hid)->delete();
            // 提交事务
            Db::commit();
            return json_encode(['status'=>200,'msg'=>'成功']);
            //=====end===============
        } catch (\Exception $e) {
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }
    /**
     * 置顶
     * h_id
     * @return string
     */
    public function maketop(){
        $hid = $_POST['h_id'];
        $time = date('Y-m-d H:i:s');
        $res = Db::table('ew_hot')->where('h_id',$hid)->update(['h_top'=>2,'h_ordertime'=>$time]);
        if($res){
            return json_encode(['status'=>200,'msg'=>'置顶成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }
    /**
     * 取消置顶
     * h_id
     * @return string
     */
    public function maketopback(){
        $hid = $_POST['h_id'];
        $time = $res = Db::table('ew_hot')->where('h_id',$hid)->value('h_time');
        $res = Db::table('ew_hot')->where('h_id',$hid)->update(['h_top'=>1,'h_ordertime'=>$time]);
        if($res){
            return json_encode(['status'=>200,'msg'=>'取消成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }

    /**
     * 删除某动态的评论的回复
     * @return string
     */
    public function delcir() {
        $cmid = $_POST['cmid'];
        // 启动事务
        Db::startTrans();
        try{
            $data = Db::table('ew_hot_comment_tt')
                ->where('comtt_id',$cmid)
                ->field('comtt_comment_id')
                ->find();
            $hid = $data['comtt_comment_id'];
            //删除掉这个动态的回复
            Db::table('ew_hot_comment_tt')->where('comtt_id',$cmid)->delete();
            Db::table('ew_hot')
                ->where('h_id',$hid)
                ->setDec('h_message');
            // 提交事务
            Db::commit();
            return json_encode(['status'=>200,'msg'=>'成功']);
            //=====end===============
        } catch (\Exception $e) {
            Db::rollback();
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }
    /**
     * 查看某用户的动态——html
     * @return string
     */
    public function myCirs() {
        $hid = $_POST['hid'];
        $this->assign ('hid',$hid);
        return $this->fetch("show/show_mes");
    }
    /**
     * 查看某用户的评论——数据
     * @return string
     */
    public function myCirsTable() {
        $hid = $_POST['hid'];
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $res = Db::table("ew_hot_comment_tt")->alias('cm')
            ->join('ew_u_user u','cm.comtt_one_user_id=u.user_id','LEFT')
            ->join('ew_u_user u2','cm.comtt_reply_user_id=u2.user_id','LEFT')
            ->field('cm.*,u.user_id one_id,u2.user_id reply_id,u.user_username one_name,u2.user_username reply_name')
            ->where('comtt_comment_id',$hid)
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table("ew_hot_comment_tt")->alias('cm')
            ->join('ew_u_user u','cm.comtt_one_user_id=u.user_id','LEFT')
            ->join('ew_u_user u2','cm.comtt_reply_user_id=u2.user_id','LEFT')
            ->field('u.user_id one_id')
            ->where('comtt_comment_id',$hid)
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }


    /*-----------------------------------------忙友榜-------------------------------------------------*/

    /**
     * 查看忙友榜——html
     * @return string
     */
    public function top() {
        $zannum = Db::table('ew_hot_top_zan')->count();
        $msgnum = Db::table('ew_hot_top_message')->count();
        $this->assign ('zannum',$zannum);
        $this->assign ('msgnum',$msgnum);
        return $this->fetch("show/top_six");
    }
    /**
     * 查看忙友榜——数据
     * @return string
     */
    public function myTopTable() {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $field = 'user_username,user_id,user_phone,user_image,user_zan';
        $res = Db::table('ew_u_user')
            ->field($field)
            ->order('user_zan DESC')
            ->limit(0,6)
            ->select();
        $data['total'] = 6;
        $data['rows'] = $res;
        return json_encode($data);
    }

    /**
     * 查看忙友榜评论——html
     * @return string
     */
    public function topmes() {
        $zannum = Db::table('ew_hot_top_zan')->count();
        $msgnum = Db::table('ew_hot_top_message')->count();
        $this->assign ('zannum',$zannum);
        $this->assign ('msgnum',$msgnum);
        return $this->fetch("show/top_mes");
    }
    /**
     * 查看忙友榜评论——数据
     * @return string
     */
    public function myTopmesTable() {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $res = Db::table("ew_hot_top_message")->alias('cm')
            ->join('ew_u_user u','cm.cm_one_user_id=u.user_id','LEFT')
            ->join('ew_u_user u2','cm.cm_reply_user_id=u2.user_id','LEFT')
            ->field('cm.*,u.user_id one_id,u2.user_id reply_id,u.user_username one_name,u2.user_username reply_name')
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table("ew_hot_top_message")->alias('cm')
            ->join('ew_u_user u','cm.cm_one_user_id=u.user_id','LEFT')
            ->join('ew_u_user u2','cm.cm_reply_user_id=u2.user_id','LEFT')
            ->field('u.user_id one_id')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }
    /**
     * 删除忙友榜的评论
     * @return string
     */
    public function deltopcir() {
        $cmid = $_POST['cmid'];
        $res = Db::table('ew_hot_top_message')->where('top_id',$cmid)->delete();
        if($res){
            return json_encode(['status'=>200,'msg'=>'成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }


    /**
     * 打榜规则
     * @return string
     */
    public function Showroles() {
        $data = Db::table('ew_personalconfig')
            ->where('id',6)->value('remark');
        $this->assign ('data',$data);
        return $this->fetch("show/show_roles");
    }
    /**
     * 打榜规则-修改
     * @return string
     */
    public function editshowroles() {
        $con = $_POST['content'];
        $res = Db::table('ew_personalconfig')
            ->where('id',6)->update(['remark'=>$con]);
        if($res){
            return json_encode(['status'=>200,'msg'=>'成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'失败']);
        }
    }

    /**
     *getUserData 页面加载获取交易记录数据
     * @return json格式数组
     */
    public function getDealData(){
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $where = $this->getDataByCampusid($_POST);
        if(isset($where['user_username1']) && $where['user_username1']){
            $u = $where['user_username1'];
            unset($where['user_username1']);
            $where['u.user_username'] = $u;
        }
        if(isset($where['user_username2']) && $where['user_username2']){
            $u2 = $where['user_username2'];
            unset($where['user_username2']);
            $where['u2.user_username'] = $u2;
        }

        $res = Db::table('ew_deal')->alias('d')
            ->join('ew_u_user u','d.d_user_id=u.user_id','LEFT')
            ->join('ew_u_user u2','d.d_who=u2.user_id','LEFT')
            ->where($where)
            ->field('d.*,u2.user_id whoid,u2.user_username whoname,u.user_id receiveid,u.user_username receivename')
            ->order('d_time DESC')
            ->limit($rows * ($page - 1), $rows)
            ->select();
        $total = Db::table('ew_deal')->alias('d')
            ->join('ew_u_user u','d.d_user_id=u.user_id','LEFT')
            ->join('ew_u_user u2','d.d_who=u2.user_id','LEFT')
            ->where($where)
            ->field('d_id')
            ->count();
        $data['total'] = $total;
        $data['rows'] = $res;
        return json_encode($data);
    }

}